package com.cmccsi.service.impl;



import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import com.cmccsi.entity.qxdeptDto;
import com.cmccsi.mapper.qxdeptDtoMapper;
import com.cmccsi.service.DeptService;
import com.cmccsi.utils.MybatisUtil;

public class DeptServiceImpl implements DeptService {
	
	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();
	private DeptRedisServiceImpl deptRedis=new DeptRedisServiceImpl();
	private Logger loger = Logger.getLogger(DomainServiceImpl.class);
	@Override
	public Boolean delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptDtoMapper deptMapper = session.getMapper(qxdeptDtoMapper.class);
			deptMapper.deleteByPrimaryKey(id);
			session.commit();
			deptRedis.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxdeptDto dept) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			
			qxdeptDtoMapper deptMapper = session.getMapper(qxdeptDtoMapper.class);
			deptMapper.updateByPrimaryKey(dept);
			session.commit();
			deptRedis.update(dept);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);

			loger.error("出错的Dept的name："+dept.getName());
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxdeptDto find(Long deptId) {
		// TODO Auto-generated method stub
		qxdeptDto dept = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptDtoMapper deptMapper = session.getMapper(qxdeptDtoMapper.class);
			dept =deptMapper.selectByPrimaryKey(deptId);
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return dept;
	}


	@Override
	public Boolean save(qxdeptDto dept) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {
			
			qxdeptDtoMapper deptMapper = session.getMapper(qxdeptDtoMapper.class);
			deptMapper.insert(dept);
			session.commit();
			deptRedis.save(dept);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
			loger.error("出错的Dept的name："+dept.getName());
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public List<qxdeptDto> findAllRootDepts() {
		// TODO Auto-generated method stub
		
		List<qxdeptDto> depts = new ArrayList<qxdeptDto>();
		SqlSession session = sessionFactory.openSession();
		try {
			qxdeptDtoMapper deptMapper = session.getMapper(qxdeptDtoMapper.class);
			depts =deptMapper.selectAllRootDepts();
			return depts;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return depts;
	}

}
